﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC05BO
    /// </summary>
    public class VYC05BO : DBConnectionBO
    {
        public VYC05BO()
            : base()
        {
            //
        }

        /// <summary>
        /// Lay danh sach cac du lieu trong VYC05
        /// </summary>
        /// <returns></returns>
        public List<ModelVYC05> GetAllList()
        {
            var list = new List<ModelVYC05>();
            const string query = "SELECT [Id],[IdLoaiChuyenMuc],[Ten],[UniqueId],[NgayCapNhat],[TrangThai] FROM [VYC-05]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC05();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.IdLoaiChuyenMuc = (dataTable.Rows[i][1] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][1]);
                model.Ten = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.UniqueId = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][4] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][4];
                model.TrangThai = (dataTable.Rows[i][5] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][5]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC05 model)
        {
            var query = "INSERT INTO [VYC-05]([IdLoaiChuyenMuc],[Ten],[UniqueId],[NgayCapNhat],[TrangThai]) VALUES(@idloaichuyenmuc, @ten, @uniqueid, GETDATE(), @trangthai)";
            var sqlCommand = new SqlCommand(query, connection);

            sqlCommand.Parameters.Add("@idloaichuyenmuc", SqlDbType.Int).Value = model.IdLoaiChuyenMuc;
            sqlCommand.Parameters.Add("@ten", SqlDbType.NVarChar).Value = model.Ten;
            sqlCommand.Parameters.Add("@uniqueid", SqlDbType.VarChar).Value = model.UniqueId;
            sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = model.TrangThai;

            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;                
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC05 model)
        {
            var query = "UPDATE [VYC-05] SET " +
                        "IdLoaiChuyenMuc = @idloaichuyenmuc" +
                        ", Ten = @ten" +
                        ",UniqueId = @uniqueid" +
                        ",NgayCapNhat = GETDATE(), TrangThai = @trangthai" +
                        " WHERE Id = @id";
            var sqlCommand = new SqlCommand(query, connection);

            sqlCommand.Parameters.Add("@idloaichuyenmuc", SqlDbType.Int).Value = model.IdLoaiChuyenMuc;
            sqlCommand.Parameters.Add("@ten", SqlDbType.NVarChar).Value = model.Ten;
            sqlCommand.Parameters.Add("@uniqueid", SqlDbType.VarChar).Value = model.UniqueId;
            sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = model.TrangThai;
            sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = model.Id;

            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC05 model)
        {
            var query = "DELETE FROM [VYC-05] " +
                        " WHERE Id = @id";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = model.Id;
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC05> GetById(int id)
        {
            var list = new List<ModelVYC05>();
            var query = "SELECT [Id],[IdLoaiChuyenMuc],[Ten],[UniqueId],[NgayCapNhat],[TrangThai] FROM [VYC-05] WHERE Id = @id";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
            var reader = sqlCommand.ExecuteReader();

            while (reader.Read())
            {
                var model = new ModelVYC05();

                model.Id = Convert.ToInt32(reader["Id"]);
                model.IdLoaiChuyenMuc = Convert.ToInt32(reader["IdLoaiChuyenMuc"]);
                model.Ten = reader["Ten"].ToString();
                model.UniqueId = reader["UniqueId"].ToString();               
                model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];
                model.TrangThai = Convert.ToInt32(reader["TrangThai"]);
                list.Add(model);
            }
            return list;
        }

        public List<ModelVYC05> GetByUniqueId(string uniqueid)
        {
            var list = new List<ModelVYC05>();
            var query = "SELECT [Id],[IdLoaiChuyenMuc],[Ten],[UniqueId],[NgayCapNhat],[TrangThai] FROM [VYC-05] WHERE UniqueId like @uniqueid";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@uniqueid", SqlDbType.VarChar).Value = uniqueid;
            var reader = sqlCommand.ExecuteReader();

            while (reader.Read())
            {
                var model = new ModelVYC05();

                model.Id = Convert.ToInt32(reader["Id"]);
                model.IdLoaiChuyenMuc = Convert.ToInt32(reader["IdLoaiChuyenMuc"]);
                model.Ten = reader["Ten"].ToString();
                model.UniqueId = reader["UniqueId"].ToString();
                model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];
                model.TrangThai = Convert.ToInt32(reader["TrangThai"]);
                list.Add(model);
            }
            reader.Close();
            return list;
        }

        public List<ModelVYC05> GetByIdLoaiChuyenMuc(int idloaicm)
        {
            var list = new List<ModelVYC05>();
            var query = "SELECT [Id],[IdLoaiChuyenMuc],[Ten],[UniqueId],[NgayCapNhat],[TrangThai] FROM [VYC-05] WHERE IdLoaiChuyenMuc = @id";
            var sqlCommand = new SqlCommand(query, connection);
            sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value = idloaicm;
            var reader = sqlCommand.ExecuteReader();

            while (reader.Read())
            {
                var model = new ModelVYC05();

                model.Id = Convert.ToInt32(reader["Id"]);
                model.IdLoaiChuyenMuc = Convert.ToInt32(reader["IdLoaiChuyenMuc"]);
                model.Ten = reader["Ten"].ToString();
                model.UniqueId = reader["UniqueId"].ToString();
                model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];
                model.TrangThai = Convert.ToInt32(reader["TrangThai"]);
                list.Add(model);
            }
            return list;
        }

    }
}